import QtQuick 2.15
import QtQuick.Controls 2.15

Rectangle {
    width: 640
    height: 480
    visible: true

    // 通用数据展示组件
    Component {
        id: listDelegate
        Column {
            spacing: 5
            Text { text: `Unique Name: ${modelData.uniqueName}`; font.bold: true }
            Text { text: `Name: ${modelData.name}` }
            Text { text: `Language: ${modelData.languageCode}` }
            Rectangle { width: parent.width; height: 1; color: "#ccc" }
        }
    }

    // 主布局
    Column {
        anchors.fill: parent
        spacing: 20
        padding: 15

        // 可用输入法列表
        GroupBox {
            title: "Available Input Methods"
            width: parent.width - 30

            Column {
                width: parent.width
                spacing: 10

                // 数据加载提示
                Text {
                    visible: inputMethodModel.availableInputMethods.length === 0
                    text: "Loading input methods..."
                    color: "gray"
                }

                // 动态生成输入法列表
                Repeater {
                    model: inputMethodModel.availableInputMethods
                    delegate: listDelegate
                }
            }
        }

        // 当前组输入法列表
        GroupBox {
            title: "Current Group Input Methods"
            width: parent.width - 30

            Column {
                width: parent.width
                spacing: 10

                Text {
                    visible: inputMethodModel.currentGroupInputMethodEntries.length === 0
                    text: "No input methods in current group"
                    color: "gray"
                }

                Repeater {
                    model: inputMethodModel.currentGroupInputMethodEntries
                    delegate: listDelegate
                }
            }
        }

        // 键盘布局列表
        GroupBox {
            title: "Keyboard Layouts"
            width: parent.width - 30

            Column {
                width: parent.width
                spacing: 10

                Text {
                    visible: inputMethodModel.availableKeyboardLayouts.length === 0
                    text: "Loading keyboard layouts..."
                    color: "gray"
                }

                Repeater {
                    model: inputMethodModel.availableKeyboardLayouts
                    delegate: Column {
                        Text { text: `Layout: ${modelData.layout}`; font.bold: true }
                        Text { text: `Description: ${modelData.description}` }
                        Text { text: "Variants:" }
                        Repeater {
                            model: modelData.variants
                            Text { text: `- ${modelData.variant} (${modelData.description})` }
                        }
                        Rectangle { width: parent.width; height: 1; color: "#ccc" }
                    }
                }
            }
        }
    }

    // 调试信息
    Component.onCompleted: {
        logDataStatus()
        // 强制刷新数据（如果C++支持）
        inputMethodModel.refreshData()
    }

    function logDataStatus() {
        console.log("System Language:", inputMethodModel.systemLanguage)
        console.log("Available Input Methods:", inputMethodModel.availableInputMethods.length)
        console.log("Current Group Entries:", inputMethodModel.currentGroupInputMethodEntries.length)
        console.log("Keyboard Layouts:", inputMethodModel.availableKeyboardLayouts.length)
    }

    // 监听所有数据变更
    Connections {
        target: inputMethodModel
        function onAvailableInputMethodsChanged() {
            console.log("Input Methods Updated")
            logDataStatus()
        }
        function onCurrentGroupInputMethodEntriesChanged() {
            console.log("Current Group Updated")
            logDataStatus()
        }
        function onAvailableKeyboardLayoutsChanged() {
            console.log("Keyboard Layouts Updated")
            logDataStatus()
        }
    }
}